package com.ssy.lingxi.platform.manage.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yzc
 * @version 2.0.0
 * @date 2020/11/19
 */
public class TrieNode {
    /**
     * 模式串末尾字符标识, false
     */
    public boolean end;
    /**
     * keyword索引
     */
    public List<Integer> results;
    /**
     * 模式串末尾节点记录keyword的索引
     */
    public Map<Character, TrieNode> children;
    /**
     * 优化hash查找
     * minflag 最小标标记
     * maxflag 最大值标记
     */
    private int minFlag = Integer.MAX_VALUE;
    private int maxFlag = 0;

    public TrieNode() {
        results = new ArrayList<>();
        children = new HashMap<>();
    }

    public void add(final char c, final TrieNode node) {
        if (minFlag > c) {
            minFlag = c;
        }
        if (maxFlag < c) {
            maxFlag = c;
        }
        children.put(c, node);
    }

    public void setResults(final Integer index) {
        if (!end) {
            end = true;
        }
        if (!results.contains(index)) {
            results.add(index);
        }
    }

    public boolean hasKey(final char c) {
        if (minFlag <= c && maxFlag >= c) {
            return children.containsKey(c);
        }
        return false;
    }

    public TrieNode getValue(final char c) {
        return children.get(c);
    }
}